﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PacManPP.Objects;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
using Microsoft.Xna.Framework.Audio;

namespace PacManPP.Managers
{
    class GameManager
    {
        static public int scale = 24;

        static public int points = 0;
        static public int lives = 3;
        static public int dots_amount = 0;

        static public Vector2 boardBegin = new Vector2(1,1);
        static public Vector2 boardSize = new Vector2(30, 24);

        public static readonly object syncLock = new object();
        private static readonly Random random = new Random();

        protected ContentManager content;
        protected SpriteBatch spriteBatch;
        protected GraphicsDevice graphics;

        public SpriteFont gameFont;

        static public List<Wall> walls;
        static public List<Dot> dots;
        static public List<Monster> monsters;
        static public List<Fruit> fruits;
        static public PacMan pacMan;

        BoardManager boardManager;
        SoundManager soundManager;

        public ContentManager Content
        {
            get { return content; }
            set { content = value; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
            set { spriteBatch = value; }
        }

        public GraphicsDevice Graphics
        {
            get { return graphics; }
            set { graphics = value; }
        }

        public static int RandomNumber(int min, int max)
        {
            lock (syncLock)
            { // synchronize
                return random.Next(min, max);
            }
        }


        public GameManager()
        {
            dots = Dot.Generate((int)boardSize.X, (int)boardSize.Y);
            dots_amount = (int)boardSize.X * (int)boardSize.Y - 5;

            monsters = new List<Monster>();

            monsters.Add(new Monster(MonsterColor.BLUE,new Vector2(GameManager.boardSize.X / 2 - 1,GameManager.boardSize.Y / 2)));
            monsters.Add(new Monster(MonsterColor.PINK,new Vector2(GameManager.boardSize.X / 2 ,GameManager.boardSize.Y / 2)));
            monsters.Add(new Monster(MonsterColor.RED,new Vector2(GameManager.boardSize.X / 2 + 1,GameManager.boardSize.Y / 2)));
            monsters.Add(new Monster(MonsterColor.YELLOW,new Vector2(GameManager.boardSize.X / 2 +2 ,GameManager.boardSize.Y / 2)));

            fruits = new List<Fruit>();
            pacMan = new PacMan();

            DirectoryInfo di = new DirectoryInfo("Maps");
            FileInfo[] rgFiles = di.GetFiles("*.xml");

            boardManager = new BoardManager(rgFiles[0].FullName);
            boardManager.readBoard();     
           

            walls = boardManager.Walls;
        }

        public void LoadContent()
        {
          

            foreach (Wall item in walls)
            {
                item.Content = content;
                item.SpriteBatch = spriteBatch;
                item.Graphics = graphics;               
                item.LoadContent();
            }

            foreach (var item in dots)
            {
                item.Content = content;
                item.SpriteBatch = spriteBatch;
                item.LoadContent();
            }

            foreach (var item in monsters)
            {
                item.Content = content;
                item.SpriteBatch = spriteBatch;
                item.LoadContent();
            }

            foreach (var item in fruits)
            {
                item.Content = content;
                item.SpriteBatch = spriteBatch;
                item.LoadContent();
            }

            pacMan.Content = content;
            pacMan.SpriteBatch = spriteBatch;
            pacMan.LoadContent();
            
        }

        public void HandleInput(KeyboardState keyboardState)
        {         
           

        }


        public void Update(GameTime gameTime)
        {

                foreach (var item in dots)
                {
                    if (item.eat_by_pacman == true && item.killed==false)
                    {
                        SoundManager.PlayDotSound();
                        item.killed = true;
                        points++;
                        dots_amount--;
                        continue;
                    }
                    item.Update(gameTime);
                }


                foreach (var item in monsters)
                {
                    item.makeMove();
                    item.Update(gameTime);
                }

                foreach (var item in fruits)
                {
                    item.Update(gameTime);
                }

                pacMan.Update(gameTime);

                BoardManager.mosterWithPacMan();
            
        }


        public void Draw(GameTime gameTime)
        {

            foreach (Wall item in walls)
            {
                item.Draw(gameTime);
            }
            
            foreach (var item in dots)
            {
                if (item.killed == false)
                    item.Draw(gameTime);
            }

            foreach (var item in monsters)
            {
                item.Draw(gameTime);
            }

            foreach (var item in fruits)
            {
                item.Draw(gameTime);
            }
            
            pacMan.Draw(gameTime);


            spriteBatch.DrawString(gameFont, "Points : " + points, new Vector2(GameManager.boardSize.X, (GameManager.boardSize.Y*GameManager.scale)+30), Color.DarkRed);
            spriteBatch.DrawString(gameFont, "Lives : " + lives, new Vector2(GameManager.boardSize.X+500, (GameManager.boardSize.Y * GameManager.scale) + 30), Color.DarkRed);
          
            
        }
    
    }

}
